package scau.erps.view.business.staff;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.RowFilter;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import scau.erps.app.Erps;
import scau.erps.entity.Loan;
import scau.erps.entity.Team;
import scau.erps.service.LoanService;
import scau.erps.service.impl.LoanServiceImpl;

/**
 * 贷款业务操作界面
 * 
 * @author DCJ
 *
 */
public class LoanPanel extends BusinessPanelTemplate {
	private static final long serialVersionUID = 1L;
	private LoanTableModel dataModel;

	public LoanPanel() {
		super();
		this.setComboItemsValue();
		this.operationBtn1.setText("还款");
		this.operationBtn2.setVisible(false);
		this.dataModel = getLoanTableModel();
		this.table.setModel(dataModel);
	}

	public LoanTableModel getLoanTableModel() {
		Team team = Erps.getCurrentTeam();
		LoanService loanService = Erps.getIocInstance().get(
				LoanServiceImpl.class);
		List<Loan> list = null;
		if (team == null) { // 工作人员登陆
			list = loanService.getAllLoans();
		} else {
			list = loanService.getLoans(team.getId(), null, null);
		}
		dataModel = new LoanTableModel(list);
		return dataModel;
	}

	@Override
	public void setComboItemsValue() {
		String[] loanType = new String[] { "贷款类型", "短贷", "长贷", "高利贷" };
		String[] loanStatus = new String[] { "贷款状态", "已申请", "已审核", "已还款" };
		this.addItems(businessType, loanType);
		this.addItems(businessStatus, loanStatus);
	}

	@Override
	/**
	 * 使用JDK6的JTable中自带RowFilter替代表查询
	 * @author FYJ
	 *
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == searchBtn) {
			String team = null;
			if (teamCombo.getSelectedIndex() != 0) {
				team = (String) teamCombo.getSelectedItem();
			}			
			String type = null;
			if (businessType.getSelectedIndex() != 0) {
				type = (String) businessType.getSelectedItem();
			}
			String status = null;
			if (businessStatus.getSelectedIndex() != 0) {
				status = (String) businessStatus.getSelectedItem();
			}
			final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(dataModel);
			List<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>(2);
			if(team !=null){
				filters.add(RowFilter.regexFilter(team));
			}
			if (type != null) {
				filters.add(RowFilter.regexFilter(type));
			}
			if (status !=null){
				filters.add(RowFilter.regexFilter(status));
			}				
			sorter.setRowFilter(RowFilter.andFilter(filters));			
			this.table.setRowSorter(sorter);
			this.table.setModel(dataModel);
		}

		// 审核贷款按钮
		else if (e.getSource() == approveBtn) {
			int row = this.table.getSelectedRow();
			if (row == -1) {
				JOptionPane.showConfirmDialog(this, "未选中任何行", "提示",
						JOptionPane.CLOSED_OPTION);
			} else {
				int loanId = (int) this.table.getValueAt(row, 0);
				LoanService loanService = Erps.getIocInstance().get(
						LoanServiceImpl.class);
				Loan loan = loanService.getLoanById(loanId);
				if ("已申请".equals(loan.getStatus())) {
					loan.setStatus("已审核");
					loanService.updateLoan(loan);
					String message = "贷款" + loan.getLoanNo() + "审批通过，请领取"
							+ loan.getAmount() + "M现金";
					JOptionPane.showConfirmDialog(this, message, "提示",
							JOptionPane.CLOSED_OPTION);
					this.table.setModel(getLoanTableModel());
				} else {
					JOptionPane.showConfirmDialog(this, "该贷款已通过审核", "提示",
							JOptionPane.CLOSED_OPTION);
				}
			}
		} else if (e.getSource() == operationBtn1) {
			// TODO 未完待续
		}
	}
}

/**
 * 表格数据模型
 * 
 * @author DCJ
 *
 */
class LoanTableModel extends AbstractTableModel {
	private static final long serialVersionUID = 1L;
	private String[] title = { "序号", "贷款单号", "小组", "贷款类型", "金额", "申请时间",
			"还款时间", "利息", "状态" };
	private Object[][] data = null;

	public LoanTableModel(List<Loan> list) {
		data = new Object[list.size()][9];
		Iterator<Loan> iter = list.iterator();
		for (int i = 0; i < list.size(); i++) {
			Loan loan = iter.next();
			data[i][0] = loan.getId();
			data[i][1] = loan.getLoanNo();
			data[i][2] = "第"+loan.getTeam()+"组";
			data[i][3] = loan.getLoanType();
			data[i][4] = loan.getAmount();
			data[i][5] = loan.getBeginTime();
			data[i][6] = loan.getEndTime();
			data[i][7] = loan.getInterest();
			data[i][8] = loan.getStatus();
		}
	}

	@Override
	public int getRowCount() {
		return this.data.length; // 行数
	}

	@Override
	public int getColumnCount() {
		return this.title.length; // 列数
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return data[rowIndex][columnIndex]; // 取表格单元格值
	}

	public String getColumnName(int columnIndex) {
		return title[columnIndex]; // 显示标题
	}
}
